var md5 = require('../../../../utils/md5.js');
var util = require('../../../../utils/util.js');
var api = require('../../../../config/api.js');
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    orderInfo: {
      type: Object,
      value: '',
      observer: function(newVal, oldVal, changedPath) {
        this.setData({
          oldOrderId: newVal.id, 
          newOrderId: newVal.id != undefined ? md5.b64Md5(newVal.id) : "",
          time: newVal.create_time != undefined ? util.formatTime(new Date(newVal.create_time)) : ""
        })
      }
    },
  },

  /**
   * 组件的初始数据
   */
  data: {
    user: false, //当前用户身份 true为导师
    newOrderId: null, //加密后的订单号
    oldOrderId: null, //加密前订单号
    time: null, //发起时间,
    countDownDay: "00",
    countDownHour: "00",
    countDownMinute: "00",
    countDownSecond: "00",
    isDisabled: false, //是否可以点击支付按钮
  },

  /**
   * 组件的方法列表
   */
  methods: {
    commit: function() {
      var myEventDetail = {
        msg: '来自component的信息'
      }
      var myEventOption = {}
      this.triggerEvent('myeventC', myEventDetail, myEventOption)
    },
    teacherCancelOrder: function() {
      if (this.data.isDisabled){
        return false;
      }
      let that = this;
      let openid = wx.getStorageSync('userInfo').openid;
      util.request(api.TeacherCancelOrder, {
        userOrderId: this.properties.orderInfo.id,
        openid: openid
      }, "PUT", false).then(function(res) {
        if (res.code === 200) {
          wx.showModal({
            content: '您已成功拒绝当前订单，点击确定返回上一页',
            success(res) {
              if (res.confirm) {
                  wx.navigateBack({
                    delta: 1
                  })
              } else if (res.cancel) {
                wx.navigateBack({
                  delta: 1
                })
              }
            },
            showCancel: false
          })
        }else{
          wx.showToast({
            title: res.message,
            icon: 'none'
          });
        }
      });
    },
    teacherAcceptOrder: function (formid) {
      if (this.data.isDisabled) {
        return false;
      }
      let that = this;
      let openid = wx.getStorageSync('userInfo').openid;
      util.request(api.TeacherAcceptOrder, {
        userOrderId: this.properties.orderInfo.id,
        openid: openid,
        formId: formid
      }, "PUT", false).then(function(res) {
        if (res.code === 200) {
          wx.navigateTo({
            url: '/myPages/process/process?userOrderId=' + that.properties.orderInfo.id
          })
        } else {
          wx.showToast({
            title: res.message,
            icon: 'none'
          });
        }
      });
    },
    cancalOrder: function () {
      if (this.data.isDisabled) {
        return false;
      }
      let openid = wx.getStorageSync('userInfo').openid;
      util.request(api.UserCancelOrder, {
        userOrderId: this.properties.orderInfo.id,
        openid: openid
      }, "PUT", false).then(function (res) {
        if (res.code === 200) {
          wx.showModal({
            content: '您已申请退款成功！您的支付金额在1个工作日将全款原路退到您的账户，请注意查收。点击确定返回首页', complete: res => {
              wx.switchTab({
                url: '/pages/index/index',
              });
            },
            showCancel: false
          })
        } else {
          wx.showToast({
            title: '退款失败,如果有疑问请联系客服人员',
            icon: 'none'
          });
        }
      });
    },
    getUserOrderTimeByIdStatus: function (userOrderId, status){
      let that = this
      util.request(api.GetUserOrderTimeByIdStatus, {
        userOrderId: userOrderId,  //订单id
        status: status  //订单状态
      }, 'GET').then(function (res){
        if (res.code == 200 || res.result.createTime != undefined){
          //取剩余24小时
          var format = res.result.createTime.replace(/-/g, '/')
          var oldTime = new Date(format);  
          // var oldTime = new Date(res.result.createTime);
          oldTime.setHours(oldTime.getHours() + 24);
          var newTime = oldTime.getTime();
          console.log(oldTime)
          that.count_down(newTime);
        }
      })
    },
    //倒计时
    count_down: function (newTime) {
      // var totalSecond = Date.parse(new Date(newTime)) / 1000 - Date.parse(new Date()) / 1000;
      var totalSecond = newTime / 1000 - Date.parse(new Date()) / 1000;
      var interval = setInterval(function () {
        // 秒数
        var second = totalSecond;

        // 天数位
        var day = Math.floor(second / 3600 / 24);
        var dayStr = day.toString();
        if (dayStr.length == 1) dayStr = '0' + dayStr;

        // 小时位
        var hr = Math.floor((second - day * 3600 * 24) / 3600);
        var hrStr = hr.toString();
        if (hrStr.length == 1) hrStr = '0' + hrStr;

        // 分钟位
        var min = Math.floor((second - day * 3600 * 24 - hr * 3600) / 60);
        var minStr = min.toString();
        if (minStr.length == 1) minStr = '0' + minStr;

        // 秒位
        var sec = second - day * 3600 * 24 - hr * 3600 - min * 60;
        var secStr = sec.toString();
        if (secStr.length == 1) secStr = '0' + secStr;

        this.setData({
          countDownDay: dayStr,
          countDownHour: hrStr,
          countDownMinute: minStr,
          countDownSecond: secStr,
        });
        totalSecond--;
        if (totalSecond < 0) {
          clearInterval(interval);
          wx.showToast({
            title: '订单已经失效',
            icon: "none"
          });
          this.setData({
            countDownDay: '00',
            countDownHour: '00',
            countDownMinute: '00',
            countDownSecond: '00',
            isDisabled: true
          });
        }
      }.bind(this), 1000);
    },
    getformid(e){
      if (this.data.isDisabled) {
        return false;
      }
      var formid = e.detail.formId
      console.log(formid);
      this.teacherAcceptOrder(formid)
    },
  },
  ready: function(e) {
    let user = false;
    if (this.properties.orderInfo.to_user_id == wx.getStorageSync('userInfo').id) {
      user = false;
    } else {
      user = true;
    }
    this.setData({
      user: user
    })

    //加载订单剩余时间
    this.getUserOrderTimeByIdStatus(this.properties.orderInfo.id,this.properties.orderInfo.status);
  }
})